/*
 * Date: 2018年10月23日 13:28:06
 * LastEditors: lvyou
 * LastEditTime: 2019年12月10日 17:22:10
 * FilePath: \youlinweb-master\src\config\util.js
 * copyright(c) 2019-2025 版权为浙江双城网络科技集团有限公司所有，违者必究。
 */
//公共方法
import api from "src/api/index";
import Vue from 'vue'
export default {
  //兰溪数据
  getlanxidata(data){
      _each(data,(v,i)=>{
        if(v.content&&v.content.indexOf("://?")!=-1){
          if(v.content.split("://?").length>3){
            let tempcontent = new Object();
            tempcontent.lanxitext = v.content.split("://?")[1];
            tempcontent.lanxiurltext = v.content.split("://?")[2];
            tempcontent.lanxiurl = v.content.split("://?")[3];
            v.lanxicontent = tempcontent;
          }else{
            v.lanxicontent = new Object();
          }
        }
      })
  },
  //返回原位置
  getTransformState(params) {
      let loadmore = document.getElementsByClassName("loadmore-ul");
        if (loadmore.length > 0) {
            // localStorage.params = loadmore[0].style.transform;
            localStorage.setItem(params,loadmore[0].style.transform);
        }
  },
  //图片自动旋转参数
  picAutoOrient(data,list) {
    for (var m = 10 * data; m < list.length; m++) {
      if(list[m].picture!=null){
        for (var n = 0; n < list[m].picture.length; n++) {
          list[m].picture[n].src =
            list[m].picture[n].src + "?imageMogr2/auto-orient";
        }
      }
    }
  },
  initScroll(thisobj,iscrollobj,callback) {//(this对象，页码等对象，网络方法)
    let p = new Promise((resolve, reject) => {
      let iscrollwrapper = thisobj.$refs[iscrollobj.wrapper];
      thisobj.$nextTick(() => {
        thisobj.myscroll = new iScroll(iscrollwrapper, {
          //上拉时触发
          onScrollMove: function() {
            if ( thisobj.myscroll.y <=  thisobj.myscroll.wrapperH -  thisobj.myscroll.scroller.clientHeight - 50 &&iscrollobj.is_r == false) {
              iscrollobj.is_r = true;
              //加载数据
              setTimeout(() => {
                if (iscrollobj.page < iscrollobj.totalPage) {
                  iscrollobj.page += 1;
                  callback();
                }
              }, 0);
            }
          },
          //上拉之后如果触发刷新则 状态图标值为1 显示loading状态
          onScrollEnd: function() { },
          onScrollStart: function() {
            if (localStorage.tempPageHeight != document.getElementsByClassName("loadmore-ul")[0].scrollHeight) { this.refresh();
              localStorage.tempPageHeight = document.getElementsByClassName( "loadmore-ul")[0].scrollHeight;
            }
          }
        });
        resolve("下啦 ...");
      });
      resolve("初始化scroll ...");
    });
    return p;
  },
  //倒计时
  getCountDown(countDownObj){
    // Set the date we're counting down to
    var countDownDate = new Date(countDownObj.counttime.replace(/-/g,'/')).getTime();
    var that = this;
   // Update the count down every 1 second
        countDownObj.timer = window.setInterval(function() {
        countDownObj.countDownTips=countDownObj.noendtip;
        // Get todays date and time
        var now = new Date().getTime();
        // Find the distance between now an the count down date
        countDownObj.distance = countDownDate - now;
        // Time calculations for days, hours, minutes and seconds
        countDownObj.days = Math.floor(countDownObj.distance / (1000 * 60 * 60 * 24))>=10?Math.floor(countDownObj.distance / (1000 * 60 * 60 * 24)):`0${Math.floor(countDownObj.distance / (1000 * 60 * 60 * 24))}`;
        countDownObj.hours = Math.floor((countDownObj.distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))>=10?Math.floor((countDownObj.distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)):`0${Math.floor((countDownObj.distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))}`;
        countDownObj.minutes = Math.floor((countDownObj.distance % (1000 * 60 * 60)) / (1000 * 60))>=10?Math.floor((countDownObj.distance % (1000 * 60 * 60)) / (1000 * 60)):`0${Math.floor((countDownObj.distance % (1000 * 60 * 60)) / (1000 * 60))}`;
        countDownObj.seconds = Math.floor((countDownObj.distance % (1000 * 60)) / 1000)>=10?Math.floor((countDownObj.distance % (1000 * 60)) / 1000):`0${Math.floor((countDownObj.distance % (1000 * 60)) / 1000)}`;

        // If the count down is over, write some text
        if (countDownObj.distance <= 0) {
            clearInterval(countDownObj.timer);
            countDownObj.countDownTips = countDownObj.endtip;
           //结束
        }
    }, 1000);
  },
  //记录点击次数
  saveclicknum(data){
    api.Queryaddtopicclicknum(data).then(res => {
      if (res.success) {}
    });
  },
  //识别文本中url
  getTextUrl(data){
    // const reg= /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;;
    const reg= /(http(s)?:\/\/.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
    // data = "你好，http://www.baidu.com,淘宝http://www.baidu.com";
     const tempurl =  data.match(reg);
     const set = new Set(tempurl);
     const newtempurl = Array.from(set);//数组去重
     let urlcode ="";
     let tempv = "";
     _each(newtempurl,(v,i)=>{
       if(v.indexOf("/?token")>-1){//过滤token
         tempv = v.substring(0,v.indexOf("/?token"));
         tempv = tempv.concat("/?token="+localStorage.getItem("token"));
       }else{
         tempv=v;
       }
       urlcode = "<a href="+tempv+" onclick='event.stopPropagation()'>网页链接</a>";
        // const reg = new RegExp(v,"g")
        // data  = data.replace( reg ,urlcode);
        data  = data.split(v).join(urlcode);
     })
     return data;
  },
  //失败数组中文本url
  getListUrl(list){
    const texturl = "<a href=";
    _each(list,(v,i)=>{
      if(v.rec_content&&v.rec_content.indexOf(texturl)==-1){//我的消息
        v.rec_content = this.getTextUrl(v.rec_content);
      }else if(v.content&&typeof v.content!=="object"&&v.content.indexOf(texturl)==-1){
        v.content = this.getTextUrl(v.content);
      }
    })
  },
  //保存用户轨迹
  saveFootPrint(obj){
    const data = {actType:obj.actType,typeValue:obj.typeValue};
    api.footprint(data).then(res=>{
      if(!res.success){
        this.$toast(res.msg);
      }
    })
  },
  appendZero(obj){
    if(obj<10) return "0" +""+ obj;
    else return obj;
  },
}

